package com.cj.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cj.model.Employee;
import com.cj.model.Post;
import com.cj.model.PostSalary;
import com.cj.model.Timecard;
import com.cj.model.common.CardName;
import com.cj.utils.CommonUtil;
import com.cj.utils.DataUtil;
import com.cj.utils.DateUtil;
import com.cj.utils.StrUtil;
import com.jfinal.core.Controller;

public class TimecardController extends Controller {
	
	public void index() {
		List<Employee> employees = Employee.me.getAllEmployees();
		List<String> cardTypes = new ArrayList<String>();
		for (CardName c : CardName.values()) {
			cardTypes.add(c.getName());
		}
		setAttr("employees", employees);
		setAttr("cards", cardTypes);
		render("timecard.jsp");
	}
	
	public void myTimecards() {
		render("myTimecard.jsp");
	}
	
	public void table() {
		Employee employee = (Employee)getSession().getAttribute("employee");
		Integer empId = employee.getInt("empId");
		Integer isAdmin = employee.getInt("isAdmin");
		List<Timecard> timecards = Timecard.me.getAllTimecards();
		List<Employee> employees = Employee.me.getAllEmployees();
		if(isAdmin == 1) {
			timecards = Timecard.me.getAllTimecards();
		} else {
			timecards = Timecard.me.getTimecardsByEmployee(empId);
		}
		List<String> cardTypes = new ArrayList<String>();
		for (CardName c : CardName.values()) {
			cardTypes.add(c.getName());
		}
		setAttr("employees", employees);
		setAttr("cards", cardTypes);
		setAttr("timecards", timecards);
		render("timecardTable.jsp");
	}
	
	public void employeeInfo() {
		String empId = getPara("id");
		Map<String, Object> jsonResult = new HashMap<>();
		Employee employee = Employee.me.getEmployeeById(Integer.parseInt(empId));
		if(employee != null) {
			Post post = Post.me.findById(employee.get("postId"));
			List<Post> posts = Post.me.getPostsByDept((Integer)post.get("deptId")); 
			jsonResult.put("status", "success");
			jsonResult.put("employee", employee);
			jsonResult.put("posts", posts);
		} else {
			jsonResult.put("status", "fail");
		}
		renderJson("result", jsonResult);
	}
	
	public void edit() {
		Map<String, Object> results = new HashMap<>();
		Timecard timecard = getModel(Timecard.class);
		setCardTime(timecard);
		timecard.update();
		Integer empId = getParaToInt("timecard.empId");
		List<Timecard> timecards = Timecard.me.getTimecardsByEmployee(empId);
		if (timecards != null && !timecards.isEmpty()) {
			results.put("events", getJsonEvents(timecards));
			results.put("flag", true);
		}
		renderJson(results);
	}
	
	public void tableEdit() {
		Timecard timecard = getModel(Timecard.class);
		setCardTime(timecard);
		timecard.update();
		redirect("/timecard/table");
	}
	
	public void delete() {
		Map<String, Object> results = new HashMap<>();
		Integer cardId = getParaToInt("cardId");
		if(Timecard.me.deleteById(cardId)) {
			results.put("flag", true);
		} else {
			results.put("flag", false);
		};
		Integer empId = getParaToInt("empId");
		List<Timecard> timecards = Timecard.me.getTimecardsByEmployee(empId);
		if (timecards != null && !timecards.isEmpty()) {
			results.put("events", getJsonEvents(timecards));
		}
		renderJson(results);
	}
	
	public void tableDelete() {
		Integer cardId = getParaToInt("cardId");
		if (Timecard.me.deleteById(cardId)) {
			renderJson("flag", true);
		} else {
			renderJson("flag", false);
		}
	}
	
	public void add() {
		Map<String, Object> results = new HashMap<>();
		Timecard timecard = getModel(Timecard.class);
		setCardTime(timecard);
		timecard.remove("cardId");
		timecard.save();
		Integer empId = getParaToInt("timecard.empId");
		List<Timecard> timecards = Timecard.me.getTimecardsByEmployee(empId);
		if (timecards != null && !timecards.isEmpty()) {
			results.put("events", getJsonEvents(timecards));
			results.put("flag", true);
		}
		renderJson(results);
	}
	
	public void tableAdd() {
		Timecard timecard = getModel(Timecard.class);
		setCardTime(timecard);
		timecard.remove("cardId");
		timecard.save();
		redirect("/timecard/table");
	}
	
	
	private void setCardTime(Timecard timecard){
		Date cardDate = timecard.getDate("cardDate");
		String cardTime = DateUtil.formatDate(cardDate, "yyyy-M");
		timecard.set("cardTime", cardTime);
	}
	
	public void info() {
		Integer cardId = getParaToInt("cardId");
		Timecard timecard = Timecard.me.findById(cardId);
		renderJson("timecard", timecard);
	}
	
	public void calCardMoney() {
		Integer empId = getParaToInt("empId");
		String cardName = getPara("cardName");
		Employee employee = Employee.me.findById(empId);
		PostSalary postSalary = null;
		if(employee != null) {
			Integer postId = employee.getInt("postId");
			postSalary = PostSalary.me.getPostSalaryByPost(postId);
			renderJson("cardMoney", DataUtil.formateMoney(getCardMoney(cardName, postSalary)));
		} else {
			renderJson("cardMoney", "");
		}
	}
	
	private double getCardMoney(String cardName, PostSalary postSalary) {
		Double daySalary = 0d;
		Double cardMoney = 0d;
		if(postSalary != null) {
			daySalary = postSalary.getDouble("basicSalary") / 30;
			if(CardName.EXTRA.getName().equals(cardName)) {
				cardMoney = daySalary * postSalary.getDouble("extraWorkRate");
			} else if(CardName.LATE.getName().equals(cardName)) {
				cardMoney = daySalary * postSalary.getDouble("lateRate");
			} else if(CardName.LEAVE.getName().equals(cardName)) {
				cardMoney = daySalary * postSalary.getDouble("leaveRate");
			} else if(CardName.NEGLECT.getName().equals(cardName)) {
				cardMoney = daySalary * postSalary.getDouble("neglectWorkRate");
			} else if(CardName.TRAVEL.getName().equals(cardName)) {
				cardMoney = daySalary * postSalary.getDouble("travelRate");
			}
		} else {
			daySalary = CommonUtil.BASIC_SALARY / 30;
			if(CardName.EXTRA.getName().equals(cardName)) {
				cardMoney = daySalary * CommonUtil.EXTRA_WORK_RATE;
			} else if(CardName.LATE.getName().equals(cardName)) {
				cardMoney = daySalary * CommonUtil.LATE_RATE;
			} else if(CardName.LEAVE.getName().equals(cardName)) {
				cardMoney = daySalary * CommonUtil.LEAVE_RATE;
			} else if(CardName.NEGLECT.getName().equals(cardName)) {
				cardMoney = daySalary * CommonUtil.NEGLECT_WORK_RATE;
			} else if(CardName.TRAVEL.getName().equals(cardName)) {
				cardMoney = daySalary * CommonUtil.TRAVEL_RATE;
			}
		}
		return cardMoney/100;
	}
	
	
	public void events() {
		Map<String, Object> resultJson = new HashMap<>();
		Integer empId = getParaToInt("empId");
		List<Timecard> timecards = Timecard.me.getTimecardsByEmployee(empId);
		if (timecards != null && !timecards.isEmpty()) {
			resultJson.put("flag", true);
			resultJson.put("events", getJsonEvents(timecards));
		} else {
			resultJson.put("flag", false);
		}
		renderJson(resultJson);
	}
	
	
	private List<Map<String, Object>> getJsonEvents(List<Timecard> timecards) {
		List<Map<String, Object>> events = new ArrayList<Map<String,Object>>();
		for (Timecard timecard : timecards) {
			Map<String, Object> event = new HashMap<String, Object>();
			event.put("id", timecard.get("cardId"));
			String cardName = timecard.get("cardName");
			String comment = timecard.getStr("cardComment");
			String title = cardName;
			if(StrUtil.notBlank(comment)) {
				title += " ----------------------- ";
				title += "备注：   ";
				title += comment;
			}
			event.put("title", title);
			String date = DateUtil.formatDate(timecard.getDate("cardDate"));
			event.put("start", date);
			event.put("end", date);
			setBackGroundColor(event, cardName);
			event.put("allDay", true);
			events.add(event);
		}
		return events;
	}
	
	private void setBackGroundColor(Map<String, Object> event, String title) {
		if(CardName.EXTRA.getName().equals(title)) {
			event.put("backgroundColor", "red");
		}
		if(CardName.LATE.getName().equals(title)) {
			event.put("backgroundColor", "yellow");
		}
		if(CardName.LEAVE.getName().equals(title)) {
			event.put("backgroundColor", "blue");
		}
		if(CardName.TRAVEL.getName().equals(title)) {
			event.put("backgroundColor", "green");
		}
		if(CardName.NEGLECT.getName().equals(title)) {
			event.put("backgroundColor", "gray");
		}
	}
	
	public void cardInfo() {
		Integer cardId = getParaToInt("id");
		Map<String, Object> jsonResult = new HashMap<>();
		Timecard timecard = Timecard.me.findById(cardId);
		if(timecard != null) {
			jsonResult.put("status", "success");
			jsonResult.put("timecard", timecard);
		} else {
			jsonResult.put("status", "fail");
		}
		renderJson("result", jsonResult);
	}
}
